package com.pro.service.imple;

import com.pro.mapper.JsLoginRecordMapper;
import com.pro.pojo.JsLoginRecord;
import com.pro.pojo.JsonData;
import com.pro.service.JsLoginRecordService;
import com.pro.utils.IPAndAddressUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author cyj
 * @date 2022.1.20$ 19.53$
 * @content :
 */
@Service
public class JsLoginRecordServiceImple implements JsLoginRecordService {
    @Autowired()
    private JsLoginRecordMapper mapper;

    /**
     * @author CYJ
     * @date 2022/1/20 18.54
     * @content : 查询登录日志
     */
    @Override
    public List<JsLoginRecord> getJsLoginRecord(Integer page,Integer limit) {
        return mapper.getJsLoginRecord(page,limit);
    }

    /**
     * @author CYJ
     * @date 2022/1/21 18.43
     * @content : 添加登录日志
     */
    @Override
    public void addJsLoginRecord(JsLoginRecord jsLoginRecord) {
        Map<String, Object> ipAndAddressUtil = IPAndAddressUtil.getIPAndAddressUtil();
        String address = ipAndAddressUtil.get("addr").toString();
        jsLoginRecord.setLoginAddress(address.substring(0,address.length()-3));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateTime = null;
        try {
            dateTime = df.parse(df.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        jsLoginRecord.setLoginDate(dateTime);
        try {
            InetAddress ip4 = Inet4Address.getLocalHost();
            jsLoginRecord.setLoginIp(ip4.toString().substring(16));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        jsLoginRecord.setLoginType(2);
        mapper.addJsLoginRecord(jsLoginRecord);
    }

    /**
     * @author CYJ
     * @date 2022/2/9 10.22
     * @content : 登录日志计数
     */
    @Override
    public Integer count() {
        return mapper.count();
    }
}
